package dgo.interval;

public class Sequence extends Interval {
    Interval[] commands;
    public Sequence(Interval[] commands){
      this.commands = commands;
      boolean b = false;
      int sum = 0;
      for(int i = 0; i < commands.length; i++){
        if(commands[i].duration == -1){
          b = true;
        }else{
          sum += commands[i].duration;
        }
      }
      
      if(b){
        this.duration = -1;
      }else{
        this.duration = sum;
      }
    }

    private void initNode(IntervalNode node){
      for(int i = 0; i < commands.length; i++){
        node.nodes.addElement(new IntervalNode(this));
      }
    }

    public boolean call(IntervalNode node){
      if(node.nodes.isEmpty()){ this.initNode(node); }
      
      while(true){
        if(node.counter >= commands.length){
          return false;
        }
        if(commands[node.counter].call((IntervalNode)node.nodes.elementAt(node.counter))){
          return true;
        }else{
          node.counter++;
        }
      }
    }
}
